package blocos;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

import blocos.BlockLogic;

@SuppressWarnings("serial")
public class BlockLogicGUI extends BlockGUI{

	private String actualPath;
	private Font fontBig = new Font("Monospaced",Font.PLAIN,16);
	private Font fontMedium = new Font("Monospaced",Font.PLAIN,12);
	private Font fontSmall = new Font("Monospaced",Font.PLAIN,8);
	private Font font;
	private String slash;
	
	public BlockLogicGUI(String path){
		
		super(); 
		if(OSDetector.isWindows()) slash = "\\";
		else slash = "/";
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(System.getProperty("user.dir")+slash+"Blocks"+slash + path + ".logic"));
	        this.actualPath =System.getProperty("user.dir")+"\\Blocks\\" + path + ".logic" ;
	        String str;
	        Hashtable<String, String> table = new Hashtable<String, String>();
	        if (in.ready()) {
	        	str = in.readLine();
	        	
	        	try {
	        		imgBig = ImageIO.read(new File(defaultImagePathBig + str));
	           		imgMedium = ImageIO.read(new File(defaultImagePathMedium + str));
	           		imgSmall = ImageIO.read(new File(defaultImagePathSmall + str));	        		
	        	}
	    	    catch(IOException e)
	    	    {
	    	    	JOptionPane.showMessageDialog(null, "Imagem nao encontrada", "Erro", JOptionPane.ERROR_MESSAGE);
	    	    }
	        }
	        while(in.ready()){
	        	str = in.readLine();
	        	table.put(str.substring(0, 3), str.substring(3,4));
	        }
	        block = new BlockLogic(table);
	        in.close();
	       
	    } 
		catch (IOException e) {
			JOptionPane.showMessageDialog(null, "Erro na leitura do arquivo de circuito logico", "Erro", JOptionPane.ERROR_MESSAGE);
		   
		}

	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		 if(scale == 1) font = fontBig;
			else if (scale == 0.75) font = fontMedium;
			else font = fontSmall;
			g.setFont(font);
			g.setColor(Color.black);
			
	   if (this.block.right!=null)
	    {
	    	g.setColor(Color.BLACK);
	    	g.drawLine(x + (int)(width*0.7), y + (int)(height*0.5), x+(int)(width*1), y+(int)(height*0.5));
	    	g.drawString(((BlockLogic)this.block).rightValue,x+(int)(width*0.90),y+(int)(height*0.46));
	    }
	    if (this.block.left!=null)
	    {
	    	g.setColor(Color.BLACK);
	    	g.drawLine(x + (int)(width*0), y + (int)(height*0.5), x+(int)(width*0.3), y+(int)(height*0.5));
	    	g.drawString(((BlockLogic)this.block).leftValue,x+(int)(width*0.02),y+(int)(height*0.46));
	    }
	    if (this.block.top!=null)
	    {
	    	g.setColor(Color.BLACK);
	    	g.drawLine(x + (int)(width*0.15), y + (int)(height*0.4), x+(int)(width*0.3), y+(int)(height*0.4));
	    	g.drawLine(x + (int)(width*0.15), y + (int)(height*0.4), x+(int)(width*0.15), y+(int)(height*0)); 
	    	g.drawString(((BlockLogic)this.block).topValue,x+(int)(width*0.18),y+(int)(height*0.25));
	    }
	    if (this.block.bottom!=null)
	    {
	    	g.setColor(Color.BLACK);
	    	g.drawLine(x + (int)(width*0.15), y + (int)(height*0.6), x+(int)(width*0.3), y+(int)(height*0.6));
	    	g.drawLine(x + (int)(width*0.15), y + (int)(height*0.6), x+(int)(width*0.15), y+(int)(height*1));
	    	g.drawString(((BlockLogic)this.block).bottomValue,x+(int)(width*0.18),y+(int)(height*0.88));
	    }
	    
	    
       
	}
	
	// persistencia 
	
	
	public String[] save() {
		String[] p = new String [4];
		p[0]="BlockLogicGUI";
		p[1]=Integer.toString(this.x);
		p[2]=Integer.toString(this.y);
		p[3]=this.actualPath;
		return p;
		
	}
	
	public BlockLogicGUI(String[] parametros){
		super(); 
		int x = Integer.parseInt(parametros[0]);
		int y = Integer.parseInt(parametros[1]);
		this.x = x;
		this.y = y ;
		
		try {
			BufferedReader in = new BufferedReader(new FileReader( parametros[2] ));
	        this.actualPath =parametros[2] ;
	        String str;
	        Hashtable<String, String> table = new Hashtable<String, String>();
	        if (in.ready()) {
	        	str = in.readLine();
	        	
	        	try {
	        		img = ImageIO.read(new File(System.getProperty("user.dir")+slash+"images"+slash+str));
	        		
	        	}
	    	    catch(IOException e)
	    	    {
	    	    	JOptionPane.showMessageDialog(null, "Imagem nao encontrada", "Erro", JOptionPane.ERROR_MESSAGE);
	    	    }
	        }
	        while(in.ready()){
	        	str = in.readLine();
	        	table.put(str.substring(0, 3), str.substring(3,4));
	        }
	        block = new BlockLogic(table);
	        in.close();
	       
	    } 
		catch (IOException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Erro na leitura do arquivo de Circuitos logicos", "Erro", JOptionPane.ERROR_MESSAGE);
		   
		}

	}

}
